package org.nobject.common.lang;

/**
 * 字节工具类
 * 
 * @author bianrongjun
 * @version 1.0
 */
public class ByteUtils {
	
	public static class ASC{
		/** TAB 制表符 */
		public static byte tab 				= (byte) 9;
		/** LF 换行 */
		public static byte lf 				= (byte) 10;
		/** CR 回车 */
		public static byte cr 				= (byte) 13;
		/** SPACE ' ' 空格 */
		public static byte space 			= (byte) 32;
		/** QUOT_D '"' 双引号 */
		public static byte quot_d 			= (byte) 34;
		/** QUOT_S ''' 单引号 */
		public static byte quot_s 			= (byte) 39;
		/** COMMA ',' 逗号 */
		public static byte comma 			= (byte) 44;
		/** DOT '.' 点 */
		public static byte dot 				= (byte) 46;
		/** COLON ':' 冒号 */
		public static byte colon 			= (byte) 58;
		/** BRACKET_L '[' 左方括号 */
		public static byte bracket_l 		= (byte) 91;
		/** SLASH_R 右斜杠 */
		public static byte slash_r 			= (byte) 92;
		/** BRACKET_R ']' 右方括号 */
		public static byte bracket_r 		= (byte) 93;
		/** BRACE_L '{' 左尖括号 */
		public static byte brace_l 			= (byte) 123;
		/** BRACE_R '}' 右尖括号 */
		public static byte brace_r 			= (byte) 125;
	}

	/**
	 * 转换转义字符
	 * 
	 * @param speChar
	 *            特殊字符
	 */
	public static String convertSlashSpecialChar(char speChar) {
		if (speChar == 't') {
			return "\t";
		} else if (speChar == 'n') {
			return "\n";
		} else if (speChar == 's') {
			return "\\s";
		} else if (speChar == '\'') {
			return "'";
		} else if (speChar == '\"') {
			return "\"";
		} else if (speChar == '\\') {
			return "\\\\";
		}
		return "\\" + new String(new char[] { speChar });
	}

	/**
	 * 截取字节数组
	 * 
	 * @param cs 字节数组
	 * @param start 起始
	 * @param c 数量
	 */
	public static byte[] subByteArray(byte[] cs, int start, int c) {
		byte[] ncs = new byte[c];
		for (int i = 0; i < c; i++) {
			ncs[i] = cs[start + i];
		}
		return ncs;
	}
	
	/** 
	 * byte数组转int
	 * @param bs byte数组
	 */
	public static int bytesToInt(byte [] bs) {
		return ((0xff & bs[0]) << 8) + bs[1];
	}

	/** 
	 * int转byte数组
	 * @param num 数字
	 */
	public static byte[] intTo2Bytes(int num) {
		byte abyte0[] = new byte[2];
		abyte0[1] = (byte) (0xff & num);		abyte0[0] = (byte) ((0xff00 & num) >> 8);
		return abyte0;
	}
	
	/** 
	 * int转byte数组
	 * @param num 数字
	 * @param bs 目标byte数组
	 */
	public static void intTo2Bytes(int num, byte[] bs) {
		bs[1] = (byte) (0xff & num);
		bs[0] = (byte) ((0xff00 & num) >> 8);
	}
	
	/** 
	 * int转byte数组
	 * @param num 数字
	 */
	public static byte[] intTo4Bytes(int num) {
		byte abyte0[] = new byte[4];
		abyte0[3] = (byte) (0xff & num);
		abyte0[2] = (byte) ((0xff00 & num) >> 8);
		abyte0[1] = (byte) ((0xff0000 & num) >> 16);
		abyte0[0] = (byte) ((0xff000000 & num) >> 24);
		return abyte0;
	}

	/** 
	 * int转byte数组
	 * @param num 数字
	 * @param bs 目标byte数组
	 */
	public static void intTo4Bytes(int num, byte bs[]) {
		bs[3] = (byte) (0xff & num);
		bs[2] = (byte) ((0xff00 & num) >> 8);
		bs[1] = (byte) ((0xff0000 & num) >> 16);
		bs[0] = (byte) (int) ((0xffffffffff000000L & (long) num) >> 24);
	}
	
	/** 
	 * long转byte数组
	 * @param num 数组
	 */
	public static byte[] longTo4Bytes(long num) {
		byte abyte0[] = new byte[4];
		abyte0[3] = (byte) (int) (255L & num);
		abyte0[2] = (byte) (int) ((65280L & num) >> 8);
		abyte0[1] = (byte) (int) ((0xff0000L & num) >> 16);
		abyte0[0] = (byte) (int) ((0xffffffffff000000L & num) >> 24);
		return abyte0;
	}

	/** 
	 * long转byte数组
	 * @param num 数字
	 * @param bs 目标byte数组
	 */
	public static void longTo4Bytes(long num, byte bs[]) {
		bs[3] = (byte) (int) (255L & num);
		bs[2] = (byte) (int) ((65280L & num) >> 8);
		bs[1] = (byte) (int) ((0xff0000L & num) >> 16);
		bs[0] = (byte) (int) ((0xffffffffff000000L & num) >> 24);
	}

	/**
	 * 4位byte数组转int
	 * @param bs 数组
	 */
	public static int bytes4ToInt(byte bs[]) {
		return (0xff & bs[0]) << 24 | (0xff & bs[1]) << 16 | (0xff & bs[2]) << 8 | 0xff & bs[3];
	}

	/**
	 * 4位byte数组转long
	 * @param bs 数组
	 */
	public static long bytes4ToLong(byte bs[]) {
		return (255L & (long) bs[0]) << 24 | (255L & (long) bs[1]) << 16 | (255L & (long) bs[2]) << 8 | 255L & (long) bs[3];
	}
}
